package com.xbongbong.paas.service.log.crm.operation.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.CrmLogMemoConstant;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.service.log.crm.operation.AbstractCrmOperationLogHandle;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.pro.log.pojo.dto.AddOperationLogDTO;
import com.xbongbong.pro.log.pojo.dto.UpdateBatchOperationLogDTO;
import com.xbongbong.pro.log.pojo.dto.UpdateOperationLogDTO;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.RedundantTemplateTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.WorkReportFieldEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author: yingjf
 * @date: 2022/4/15 14:41
 * @description:工作报告
 */
@Slf4j
@Service("workReportOperationLogHandleImpl")
public class WorkReportOperationLogHandleImpl extends AbstractCrmOperationLogHandle {
    @Override
    public List<Integer> type() {
        return Arrays.asList(
                XbbRefTypeEnum.WORK_REPORT.getCode(),
                XbbRefTypeEnum.WORKREPORT_DAILY.getCode(),
                XbbRefTypeEnum.WORKREPORT_WEEKLY.getCode(),
                XbbRefTypeEnum.WORKREPORT_MONTHLY.getCode()
        );
    }

    @Override
    @Async("sysLogThreadPool")
    public void add(AddOperationLogDTO addOperationLogDTO) {
        try {
            FormDataAddDTO formDataAddDTO = addOperationLogDTO.getFormDataAddDTO();
            PaasFormDataEntity newPaasFormDataEntity = addOperationLogDTO.getNewPaasFormDataEntity();
            SaveVO saveVO = addOperationLogDTO.getSaveVO();
            BeforeSaveVO beforeSaveVO = addOperationLogDTO.getBeforeSaveVO();
            boolean isImport = Objects.equals(1, formDataAddDTO.getIsImport());
            if (isImport) {
                return;
            }
            JSONObject data = newPaasFormDataEntity.getData();
            Long reportDate = FastJsonHelper.getLongOrDefaultFromFormData(data, WorkReportFieldEnum.REPORT_DATE.getAttr(), 0L);
            String reportName = DateTimeUtil.getStringEpochSecond(reportDate, DateTimeUtil.SDFDate);
            RedundantTemplateTypeEnum workReportTypeEnum = RedundantTemplateTypeEnum.getByCode(formDataAddDTO.getSubBusinessType());
            String logName = "";
            switch (workReportTypeEnum) {
                case WORKREPORT_DAILY:
                    logName = "日报(“" + reportName + "“)";
                    break;
                case WORKREPORT_WEEKLY:
                    logName = "周报(“" + reportName + "“)";
                    break;
                case WORKREPORT_MONTHLY:
                    logName = "月报(“" + reportName + "“)";
                    break;
                default:
                    break;
            }
            String memoName = beforeSaveVO.getPaasFormEntityExt().getName();
            addSaveLogger(formDataAddDTO, newPaasFormDataEntity, logName, memoName, OperateModuleTypeEnum.WORKREPORT, saveVO, beforeSaveVO);
        } catch (XbbException e) {
            log.error("com.xbongbong.paas.service.log.operation.impl.WorkReportOperationLogHandleImpl.add fail", e);
        }
    }

    @Override
    @Async("sysLogThreadPool")
    public void update(UpdateOperationLogDTO updateOperationLogDTO) {
        try {
            FormDataUpdateDTO formDataUpdateDTO = updateOperationLogDTO.getFormDataUpdateDTO();
            UpdateVO updateVO = updateOperationLogDTO.getUpdateVO();
            BeforeUpdateVO beforeUpdateVO = updateOperationLogDTO.getBeforeUpdateVO();
            RedundantTemplateTypeEnum redundantTemplateTypeEnum = RedundantTemplateTypeEnum.getByCode(formDataUpdateDTO.getBusinessType());
            boolean isCoverage = Objects.equals(1, formDataUpdateDTO.getIsCover());
            if(isCoverage){
                return;
            }
            // 记录日志
            PaasFormDataEntity paasFormDataEntity = updateVO.getPaasFormDataEntity();
            Long reportDate = FastJsonHelper.getLongOrDefaultFromFormData(paasFormDataEntity.getData(), WorkReportFieldEnum.REPORT_DATE.getAttr(), 0L);
            String reportSec = DateTimeUtil.getStringEpochSecond(reportDate, DateTimeUtil.SDFDate);
            String memoName = beforeUpdateVO.getPaasFormEntityExt().getName();
            String userName = updateVO.getUserVO().getName();
            String opObjectName = "";
            RedundantTemplateTypeEnum workReportTypeEnum = RedundantTemplateTypeEnum.getByCode(formDataUpdateDTO.getSubBusinessType());
            switch (workReportTypeEnum) {
                case WORKREPORT_DAILY:
                    opObjectName = "日报(“" + reportSec + "“)";
                    break;
                case WORKREPORT_WEEKLY:
                    opObjectName = "周报(“" + reportSec + "“)";
                    break;
                case WORKREPORT_MONTHLY:
                    opObjectName = "月报(“" + reportSec + "“)";
                    break;
                default:
                    break;
            }
            String memo = String.format(I18nMessageUtil.getMessage(CrmLogMemoConstant.LOG_MEMO_USER_UPDATE_WORK), userName, memoName, opObjectName);
            updateSaveLogger(formDataUpdateDTO, paasFormDataEntity, redundantTemplateTypeEnum, memo, opObjectName, OperateModuleTypeEnum.WORKREPORT, updateVO, beforeUpdateVO,
                    OperateTypeEnum.EDIT, IndexTypeEnum.IDX_SAAS_WORK_REPORT_SUB);
        } catch (XbbException e) {
            log.error("com.xbongbong.paas.service.log.operation.impl.WorkReportOperationLogHandleImpl.update", e);
        }
    }

    @Override
    @Deprecated
    @Async("sysLogThreadPool")
    public void updateBatch(UpdateBatchOperationLogDTO updateBatchOperationLogDTO) {
        //工作报告无批量编辑
    }

}
